$(document).ready(function(){
    //Sort
    $('.sym_blog_admin ul').sortable({
        handle: '.sort',
        placeholder: 'ui-state-highlight',
        update: function(){
            var sort=new Array();
            $('.sym_blog_admin ul li div.id').each(function(){
                sort.push($(this).html());
            });
            $.ajax({
                data:{
                    'symbiont': 'blog-admin.db_sort',
                    'sort': sort
                },
                success:function(r){
                    
                },
                dataType: 'html'
            });
        }
    });
    //Add
    $('.sym_blog_admin .symbiosis-button-add').click(function(){
        category_id=$('.sym_blog_category_id').text();
        if($('#sym_blog_add').length){
            $('#sym_blog_add').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_blog_add" title="Note adding"></div>');
            $('#sym_blog_add').dialog({
                modal: false,
                width: $('body').width()-200,
                position: ['center', 50],
                open: function(event, ui) {
                    $.ajax({
                        data:{
                            symbiont: 'blog-admin.add.'+category_id
                        },
                        success:function(r){
                            $('#sym_blog_add').html(r).sym();
                            $('#sym_blog_add form').submit(function(){
                                $('#sym_blog_add').parent().find('.ui-dialog-buttonpane button:last').click();
                                return false;
                            });
                            $('.sym_blog_add .tabs')
                            .tabs();
                            i=0;
                            $('.sym_blog_add .wysiwyg').each(function(){
                                $('.sym_blog_add .tabs').tabs('option', 'selected', i);
                                $(this).wysiwyg();
                                i++;
                            });
                            $('.sym_blog_add .tabs').tabs('option', 'selected', 0);
                        },
                        dataType: 'html'
                    });
                },
                close: function(event, ui) {
                    $(this).html('');
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        t=this;
                        title={};
                        description={};
                        $(t).find('.title').each(function(){
                            title[$(this).attr('title')]=$(this).val();
                        });
                        $(t).find('.description').each(function(){
                            description[$(this).attr('title')]=$(this).val();
                        });
                        $('#sym_blog_add .message').removeClass('ui-state-error').html('');
                        if($('#sym_blog_add_image').val()){
                            $.ajaxFileUpload({
                                url:'?page=$&upload&name=image&dir=uploads/images/',
                                secureuri:false,
                                fileElementId:'sym_blog_add_image',
                                dataType: 'json',
                                success: function (data, status){
                                    if(typeof(data.error) != 'undefined'){
                                        if(data.error != ''){
                                            alert(data.error);
                                        }
                                        else{
                                            $.ajax({
                                                data:{
                                                    'symbiont': 'blog-admin.db_add',
                                                    'image': 'uploads/images/'+data.name,
                                                    'title': title,
                                                    'description': description,
                                                    'name': $(t).find('.name').val(),
                                                    'category_id': $(t).find('.parent').val(),
                                                    'price': $(t).find('.price').val()
                                                },
                                                success:function(r){
                                                    if(r.ok!=undefined){
                                                        if(r.attention!=undefined&&r.attention){
                                                            $('#sym_blog_add .message').addClass('ui-state-highlight').html(r.attention);
                                                            setTimeout(function(){
                                                                location.reload(true);
                                                            }, 1000);
                                                        }
                                                        else{
                                                            $(t).dialog("close");
                                                            location.reload(true);
                                                        }
                                                    }
                                                    else if(r.error!=undefined){
                                                        $('#sym_blog_add .message').addClass('ui-state-error').html(r.error);
                                                    }
                                                }
                                            });
                                        }
                                    }
                                },
                                error: function (data, status, e){
                                    alert(e);
                                }
                            });
                        }
                        else{
                            $.ajax({
                                data:{
                                    'symbiont': 'blog-admin.db_add',
                                    'image': '',
                                    'title': title,
                                    'description': description,
                                    'name': $(t).find('.name').val(),
                                    'category_id': $(t).find('.parent').val(),
                                    'price': $(t).find('.price').val()
                                },
                                success:function(r){
                                    if(r.ok!=undefined){
                                        if(r.attention!=undefined&&r.attention){
                                            $('#sym_blog_add .message').addClass('ui-state-highlight').html(r.attention);
                                            setTimeout(function(){
                                                location.reload(true);
                                            }, 1000);
                                        }
                                        else{
                                            $(t).dialog("close");
                                            location.reload(true);
                                        }
                                    }
                                    else if(r.error!=undefined){
                                        $('#sym_blog_add .message').addClass('ui-state-error').html(r.error);
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }
    });
    //Edit
    $('.sym_blog_admin .edit').click(function(){
        t=this;
        id=($(t).parent().parent().find('.id').first().text());
        if($('#sym_blog_edit').length){
            $('#sym_blog_edit').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_blog_edit" title="Note editing"></div>');
            $('#sym_blog_edit').dialog({
                modal: false,
                width: $('body').width()-200,
                position: ['center', 50],
                open: function(event, ui) {
                    $.ajax({
                        data:{
                            symbiont: 'blog-admin.edit.'+id
                        },
                        success:function(r){
                            $('#sym_blog_edit').html(r).sym();
                            $('#sym_blog_add form').submit(function(){
                                $('#sym_blog_add').parent().find('.ui-dialog-buttonpane button:last').click();
                                return false;
                            });
                            $('.sym_blog_edit .tabs')
                            .tabs();
                            i=0;
                            $('.sym_blog_edit .wysiwyg').each(function(){
                                $('.sym_blog_edit .tabs').tabs('option', 'selected', i);
                                $(this).wysiwyg();
                                i++;
                            });
                            $('.sym_blog_edit .tabs').tabs('option', 'selected', 0);
                        },
                        dataType: 'html'
                    });
                },
                close: function(event, ui) {
                    $(this).html('');
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        t=this;
                        title={};
                        description={};
                        $(t).find('.title').each(function(){
                            title[$(this).attr('title')]=$(this).val();
                        });
                        $(t).find('.description').each(function(){
                            description[$(this).attr('title')]=$(this).val();
                        });
                        $('#sym_blog_edit .message').removeClass('ui-state-error').html('');
                        if($('#sym_blog_edit_image').val()){
                            $.ajaxFileUpload({
                                url:'?page=$&upload&name=image&dir=uploads/images/',
                                secureuri:false,
                                fileElementId:'sym_blog_edit_image',
                                dataType: 'json',
                                success: function (data, status){
                                    if(typeof(data.error) != 'undefined'){
                                        if(data.error != ''){
                                            alert(data.error);
                                        }
                                        else{
                                            $.ajax({
                                                data:{
                                                    'symbiont': 'blog-admin.db_edit',
                                                    'image': 'uploads/images/'+data.name,
                                                    'title': title,
                                                    'description': description,
                                                    'name': $(t).find('.name').val(),
                                                    'category_id': $(t).find('.parent').val(),
                                                    'price': $(t).find('.price').val(),
                                                    'id': id
                                                },
                                                success:function(r){
                                                    if(r.ok!=undefined){
                                                        if(r.attention!=undefined&&r.attention){
                                                            $('#sym_blog_edit .message').addClass('ui-state-highlight').html(r.attention);
                                                            setTimeout(function(){
                                                                location.reload(true);
                                                            }, 1000);
                                                        }
                                                        else{
                                                            $(t).dialog("close");
                                                            location.reload(true);
                                                        }
                                                    }
                                                    else if(r.error!=undefined){
                                                        $('#sym_blog_edit .message').addClass('ui-state-error').html(r.error);
                                                    }
                                                }
                                            });
                                        }
                                    }
                                },
                                error: function (data, status, e){
                                    alert(e);
                                }
                            });
                        }
                        else{
                            $.ajax({
                                data:{
                                    'symbiont': 'blog-admin.db_edit',
                                    'image': '',
                                    'title': title,
                                    'description': description,
                                    'name': $(t).find('.name').val(),
                                    'category_id': $(t).find('.parent').val(),
                                    'price': $(t).find('.price').val(),
                                    'id': id
                                },
                                success:function(r){
                                    if(r.ok!=undefined){
                                        if(r.attention!=undefined&&r.attention){
                                            $('#sym_blog_edit .message').addClass('ui-state-highlight').html(r.attention);
                                            setTimeout(function(){
                                                location.reload(true);
                                            }, 1000);
                                        }
                                        else{
                                            $(t).dialog("close");
                                            location.reload(true);
                                        }
                                    }
                                    else if(r.error!=undefined){
                                        $('#sym_blog_edit .message').addClass('ui-state-error').html(r.error);
                                    }
                                }
                            });
                        }
                    }
                }
            });
        }
    });
    //Delete
    $('.sym_blog_admin .delete').click(function(){
        t=this;
        if($('#sym_blog_delete').length){
            $('#sym_blog_delete').dialog('open');
        }
        else{
        $('#windows').append('<div id="sym_blog_delete" title="Note deliting">Are you sure?</div>');
            $('#sym_blog_delete').dialog({
                modal: true,
                width: 200,
                open: function(event, ui) {
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        $(this).dialog("close");
                        $.ajax({
                            data:{
                                'symbiont': 'blog-admin.db_delete',
                                'id': $(t).parent().parent().find('.id').first().text()
                            },
                            success:function(r){
                                $(t).parent().parent().remove();
                            },
                            dataType: 'html'
                        });
                    }
                }
            });
        }
    });
});